<template>
  <div>
    <el-dialog
      width="85%"
      :title="$t('详情')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel"
    >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <!-- 订单号 -->
        <el-form-item label="订单号" prop="orderNo">
          <el-input
            v-model="queryParams.orderNo"
            clearable
            @keyup.enter.native="getList"
          />
        </el-form-item>
        <!-- 店铺名称 -->
        <el-form-item label="店铺名称" prop="shopName">
          <el-input
            v-model="queryParams.shopName"
            clearable
            @keyup.enter.native="getList"
          />
        </el-form-item>
        <!-- 租户名称 -->
        <el-form-item label="租户名称" prop="distrName">
          <el-input
            v-model="queryParams.distrName"
            clearable
            @keyup.enter.native="getList"
          />
        </el-form-item>
        <!-- 姓 -->
        <el-form-item label="姓" prop="familyName">
          <el-input
            v-model="queryParams.familyName"
            clearable
            @keyup.enter.native="getList"
          />
        </el-form-item>
        <!-- 购买时间 -->
        <el-form-item label="购买时间" prop="purchaseDate">
          <el-date-picker
            style="width: 400px"
            v-model="queryParams.time"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getList"
          >
            <!-- 搜索 -->
            {{ $t("搜索") }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            <!-- 重置 -->
            {{ $t("重置") }}
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        :height="calTableHeight() - 200"
        ref="mianRef"
        v-loading="loading"
          :data="tableData"
      >
        <!-- 订单号 -->
        <el-table-column
          show-overflow-tooltip
          label="订单号"
          width="150"
          align="center"
          prop="orderNo"
        />
        <!-- 店铺名称 -->
        <el-table-column
          show-overflow-tooltip
          label="店铺名称"
          align="center"
          prop="shopName"
        />
        <!-- 租户名称 -->
        <el-table-column
          show-overflow-tooltip
          label="租户名称"
          align="center"
          prop="distrName"
        />
        <!-- 商品名称 -->
        <el-table-column
          show-overflow-tooltip
          label="商品名称"
          width="250"
          align="center"
          prop="itemName"
        >
          <template slot-scope="{ row }">
            <el-input
              :autosize="{ minRows: 1, maxRows: 2 }"
              type="textarea"
              v-model="row.itemName"
            />
          </template>
        </el-table-column>
        <!-- 价格 -->
        <el-table-column
          show-overflow-tooltip
          label="价格"
          align="center"
          prop="price"
        />
        <!-- 支付金额 -->
        <el-table-column
          show-overflow-tooltip
          label="支付金额"
          align="center"
          prop="paymentFee"
        />
        <!-- 支付价格 -->
        <el-table-column
          show-overflow-tooltip
          label="支付价格"
          align="center"
          prop="price"
        />
        <!-- 购买时间 -->
        <el-table-column
          show-overflow-tooltip
          label="购买时间"
          align="center"
          prop="purchaseDate"
        />
        <!-- 订单状态 -->
        <el-table-column
          show-overflow-tooltip
          label="订单状态"
          align="center"
          prop="status"
        />

        <!-- 邮编 -->
        <el-table-column show-overflow-tooltip label="邮编" align="center">
          <template slot-scope="{ row }">
            {{ `〒${row.zipCode1 || ""}-${row.zipCode2 || ""}` }}
          </template>
        </el-table-column>
        <!-- 姓名 -->
        <el-table-column show-overflow-tooltip label="姓名" align="center">
          <template slot-scope="{ row }">
            {{ `${row.familyName || ""} ${row.familyNameKana || ""}` }}
          </template>
        </el-table-column>
        <!-- 地址 -->
        <el-table-column
          show-overflow-tooltip
          label="地址"
          align="center"
          prop="purchaseDate"
        >
          <template slot-scope="{ row }">
            {{ handleddress(row) }}
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column
          :label="$t('操作')"
          align="center"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-button
              v-hasPermi="[
                'distribution:orderMercari:updateOrderMercariIndividual',
              ]"
              @click="handleEdit(row)"
              type="text"
            >
              修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  updateOrderMercariIndividual,
} from "@/api/orderManage/mercariPersonalSync";

export default {
  activated() {
    this.$nextTick(() => this.$refs['mianRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: {},
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      queryParams: {
        time: [],
      },
      form: {},
      // 表单校验
      rules: {
        distrName: [{ required: true, message: "请输入", trigger: "blur" }],
      },
      tableData: [],
      // 遮罩层
      loading: true,
      total: 0,
    };
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.queryParams.time = [];
        this.getList();
      }
    },
  },
  methods: {
    handleEdit(row) {
      const data = {
        id: row.id,
        itemName: row.itemName,
      };
      updateOrderMercariIndividual(data).then((res) => {
        const { code, data, msg } = res;
        if (code === 200) {
          this.$message({ type: 'success', message: msg})
          this.getList();
        }
      });
    },
    handleddress(row) {
      return (
        (row.prefecture || "") +
        (row.city || "") +
        (row.address1 || "") +
        (row.address2 || "")
      );
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach(
        (key) => (this.queryParams[key] = "")
      );
      this.queryParams.pageSize = 10;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询供应厂家列表 */
    getList() {
      /****************** 处理查询条件 *********************/
      const selectDate = this.queryParams.time || [];
      const createTimeStart = selectDate[0] || "";
      const createTimeEnd = selectDate[1] || "";
      const data = {
        ...this.queryParams,
        createTimeStart,
        createTimeEnd,
      };
      delete data.time;
      /**************** 处理查询条件 end *******************/
      this.loading = true;
      getList({ ...data, recordsId: this.formData.recordsId }).then(
        (response) => {
          this.tableData = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    handleSubmit() {
      this.$refs["formRef"].validate((valid) => {
        if (valid) {
        }
      });
    },
    cancel() {
      this.$emit("update:show", false);
    },
  },
};
</script>
<style scoped lang="scss"></style>
